package com.hbnu.system.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hbnu.system.config.BaseException;
import com.hbnu.system.mapper.ProjectCateMapper;
import com.hbnu.system.model.entity.Projectcate;
import com.hbnu.system.service.ProjectCateService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

@Service
public class ProjectCateServiceImpl extends ServiceImpl<ProjectCateMapper, Projectcate> implements ProjectCateService {
    @Override
    public boolean addProjectCate(Projectcate projectcate) {
        Projectcate projectcate1 = super.getOne(Wrappers.lambdaQuery(Projectcate.class)
                .eq(Projectcate::getProjectCateName,projectcate.getProjectCateName()));
        if(StrUtil.isEmpty(projectcate.getProjectCateName())||StrUtil.isBlank(projectcate.getProjectCateName())){
            throw new BaseException("该项目类别名称为空");
        }
        if(!Objects.isNull(projectcate1)){
            throw new BaseException("该项目类别名称已存在");
        }
        return baseMapper.insert(projectcate)==1;
    }

    @Override
    public List<Projectcate> getProjectCateList() {
        List<Projectcate> depts = baseMapper.selectList(Wrappers.lambdaQuery(Projectcate.class));
        return depts;
    }

    @Override
    public boolean updateProjectCate(Projectcate projectcate) {
        if(StrUtil.isEmpty(projectcate.getProjectCateName())||StrUtil.isBlank(projectcate.getProjectCateName())){
            throw new BaseException("该项目类别名称为空");
        }
        if(baseMapper.updateById(projectcate)==1){
            return true;
        }
        return false;
    }

    @Override
    public boolean deleteProjectCate(String ID) {
        return baseMapper.deleteById(ID) == 1;
    }

    @Override
    public List<Projectcate> getByProjectCateName(String projectcateName) {
        LambdaQueryWrapper<Projectcate> queryWrapper = Wrappers.lambdaQuery(Projectcate.class);

        // 如果 projectcateName 不为空且不是空白字符串，则添加模糊匹配条件
        if (StringUtils.isNotBlank(projectcateName)) {
            queryWrapper.like(Projectcate::getProjectCateName, projectcateName);
        }

        return baseMapper.selectList(queryWrapper);
    }
}
